﻿using il2lua.Services;

using Mono.Cecil;
namespace il2lua;

public class TypeContext
{
    private readonly ModuleContext _up;
    private readonly Dictionary<string, ILuaRole> _fields;
    private readonly Dictionary<string, ILuaRole> _methods;
    private readonly INameService _nameService;
    public TypeContext(ModuleContext up)
    {
        _nameService = Services.Services.GetService<INameService>();
        _up = up;
    }
    public ILuaRole GetRole(FieldReference field)
    {
        var key = field.FullName;
        if (_fields.TryGetValue(key, out var role))
            return role;
        var typeRole =GetRole(field.DeclaringType);
        role = new LuaRole(field.Name,typeRole.FullName);
        return _fields[key] = role;
    }
    public ILuaRole GetRole(FieldDefinition field)
    {
        var key = field.FullName;
        if (_fields.TryGetValue(key, out var role))
            return role;
        var typeRole =GetRole(field.DeclaringType);
        role = new LuaRole(field.Name,typeRole.FullName);
        return _fields[key] = role;
    }
    public ILuaRole GetRole(MethodReference method)
    {
        var key = method.FullName;
        if (_methods.TryGetValue(key, out var role))
            return role;
        var typeRole =GetRole(method.DeclaringType);
        role = new LuaRole(method.Name,typeRole.FullName);
        return _methods[key] = role;
    }
    public ILuaRole GetRole(MethodDefinition method)
    {
        var key = method.FullName;
        if (_methods.TryGetValue(key, out var role))
            return role;
        var typeRole =GetRole(method.DeclaringType);
        role = new LuaRole(method.Name,typeRole.FullName);
        return _methods[key] = role;
    }
    public ILuaRole GetRole(TypeDefinition type)
    {
        return _up.GetRole(type);
    }
    public ILuaRole GetRole(TypeReference type)
    {
        return _up.GetRole(type);
    }
}